package com.usefullc.blog.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.usefullc.blog.enums.ArticleStatusEnum;
import com.usefullc.blog.param.ArticleStatusParam;
import com.usefullc.common.enums.YesNoEnum;
import com.usefullc.system.validator.UsefulValid;
import com.usefullc.blog.domain.Article;
import com.usefullc.blog.param.ArticleParam;
import com.usefullc.blog.vo.ArticleVo;
import com.usefullc.blog.query.ArticleQuery;
import com.usefullc.blog.service.ArticleService;
import com.usefullc.common.util.BeanUtils;
import com.usefullc.system.controller.BaseController;
import com.usefullc.system.controller.WebResultBuilder;
import com.usefullc.system.entity.Pager;
import com.usefullc.system.entity.WebResult;
import com.usefullc.system.param.IdsParam;
import com.usefullc.system.vo.TreeVo;
import com.usefullc.user.service.component.UserManager;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

import java.util.List;

import static com.usefullc.system.controller.WebResultBuilder.successResult;

/**
 * 文章控制类
 * @author: 星空
 * @date: 20220628
 */
@RestController
@RequestMapping("/blog/article/")
@Api( tags="文章")
public class ArticleController extends BaseController {

    @Autowired
    private ArticleService articleService;

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "文章列表",response = WebResult.class)
    @GetMapping(path = "list",produces = "application/json")
    public WebResult list(@UsefulValid(query = true) ArticleQuery query) {
        Pager<ArticleVo> page = articleService.findVoByPage(query);
        return WebResultBuilder.successResult(page);
    }


    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "获取所有列表", response = WebResult.class)
    @GetMapping(path = "listAll", produces = "application/json")
    public WebResult listAll(@UsefulValid(query = true) ArticleQuery query) {
        List<Article> dataList = articleService.findAll(query);
        return WebResultBuilder.successResult(dataList);
    }


    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "获取树列表", response = WebResult.class)
    @GetMapping(path = "listTree", produces = "application/json")
    public WebResult listTree(@UsefulValid(query = true) ArticleQuery query) {
        List<TreeVo> treeVoList = articleService.listTree(query);
        return WebResultBuilder.successResult(treeVoList);
    }

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "文章详情",response = WebResult.class)
    @GetMapping(path = "detail",produces = "application/json")
    public WebResult detail(@ApiParam(value = "示例ID") @Valid @RequestParam Long id) {
        Article article = articleService.findById(id);
        return WebResultBuilder.successResult(article);
    }


    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "创建文章", response = WebResult.class)
    @PostMapping(path = "/save", produces = "application/json")
    @UsefulValid
    public WebResult save(@UsefulValid(save=true) @RequestBody ArticleParam param) {
        param.setId(null);
        boolean result = articleService.saveArticle(param);
        return successResult(result);
    }

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "修改文章", response = WebResult.class)
    @PostMapping(path = "/update", produces = "application/json")
    public WebResult update(@UsefulValid(update=true) @RequestBody ArticleParam param) {
        boolean result = articleService.saveArticle(param);
        return successResult(result);
    }

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @PostMapping(path = "del",produces = "application/json")
    @ApiOperation(value = "删除文章",response = WebResult.class)
    public WebResult del(@UsefulValid(del=true) @RequestBody IdsParam idParam) {
        articleService.delete(idParam.getIds());
        return WebResultBuilder.successResult();
    }

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @PostMapping(path = "delByLogic",produces = "application/json")
    @ApiOperation(value = "逻辑删除文章",response = WebResult.class)
    public WebResult delByLogic(@UsefulValid(del=true) @RequestBody IdsParam idParam) {
        articleService.delete(idParam.getIds());
        return WebResultBuilder.successResult();
    }




    @ApiOperation(value = "设置开启回复", response = WebResult.class)
    @PostMapping(path = "setReply", produces = "application/json")
    public WebResult setReply(@UsefulValid @RequestBody ArticleStatusParam param) {
        articleService.getBaseMapper().setReply(param.getIds(), param.getStatus());
        return WebResultBuilder.successResult();
    }

    @ApiOperation(value = "设置推荐", response = WebResult.class)
    @PostMapping(path = "setRecommend", produces = "application/json")
    public WebResult setRecommend(@UsefulValid @RequestBody ArticleStatusParam param) {
        articleService.getBaseMapper().setRecommend(param.getIds(), param.getStatus());
        return WebResultBuilder.successResult();
    }

    @ApiOperation(value = "设置置顶", response = WebResult.class)
    @PostMapping(path = "setTop", produces = "application/json")
    public WebResult setTop(@UsefulValid @RequestBody ArticleStatusParam param) {
        articleService.getBaseMapper().setTop(param.getIds(), param.getStatus());
        return WebResultBuilder.successResult();
    }

    @ApiOperation(value = "修改投稿状态", response = WebResult.class)
    @PostMapping(path = "updateContributionStatus", produces = "application/json")
    public WebResult updateContributionStatus(@UsefulValid @RequestBody ArticleStatusParam param) {
        articleService.getBaseMapper().updateContributionStatus(param.getIds(), param.getStatus());
        return WebResultBuilder.successResult();
    }

    @ApiOperation(value = "文章状态修改", response = WebResult.class)
    @PostMapping(path = "updateStatus", produces = "application/json")
    public WebResult updateStatus(@UsefulValid @RequestBody ArticleStatusParam param) {
        articleService.getBaseMapper().updateStatus(param.getIds(), param.getStatus());
        return WebResultBuilder.successResult();
    }




}
